var server = "cdt"
var lodingNumber = 1;
var lodingActiveMovie = undefined;
var access_token = "";
var requestLast = "&callback=?&platform=weixin";
var lat = 0;
var lng = 0;

//写cookies
$.setCookie = function(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()+";path=/;";
}

//读取cookies
$.getCookie = function(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}

//请求调用微信。js
$(document).ready(function(){
	var signature = undefined;	
//	alert($('.money').val());
//  alert(window.location.href);
	
	$.getSignature = function(){
		var url = encodeURIComponent(window.location.href);
		console.log(url);
		
		//生成微信访问token并配置
	    $.getJSON("/"+server+"/weixinOpenPlatformToken?callback=?&url="+url+requestLast,function(json){
	    	// var tokenJSON = eval('('+json+')');
	    	signature = json;
			wx.checkJsApi({
		    	jsApiList: ['chooseImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
		    	success: function(res) {
		        // 以键值对的形式返回，可用的api值true，不可用为false
		        // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
		    	}
			});
//	    	alert(signature.signature);
	    	wx.config({
	    		debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
	    		appId: 'wxbc92da7e712a95fd', // 必填，公众号的唯一标识
	    		timestamp: signature.timestamp, // 必填，生成签名的时间戳
	    		nonceStr: signature.noncestr, // 必填，生成签名的随机串
	    		signature: signature.signature,// 必填，签名，见附录1
	    		jsApiList: ['scanQRCode','getLocation'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
			});	
			wx.error(function(res){

			});	
		
         });   
	}
	$.getSignature();


});

$.lodingActive = function(){
	$('.pop .pop-tag .pop-content1').css('padding-top','0em');
	$('.loding1').css('background','#565656');
	$('.loding2').css('background','#565656');
	$('.loding3').css('background','#565656');
	$('.loding'+lodingNumber).css('background','#00CEFC');
	if(lodingNumber < 3){
		lodingNumber = lodingNumber+1;
	}
	else{
		lodingNumber = 1;	
	}
}

//隐藏提示框
$.popTagHide = function(){
	$('.pop').hide();
	if(lodingActiveMovie != undefined){
		clearInterval(lodingActiveMovie);
	}
}

//隐藏提示框并调回公众号首页
$.popTagHide2 = function(){
	$('.pop').hide();
	if(lodingActiveMovie != undefined){
		clearInterval(lodingActiveMovie);
	}
	wx.closeWindow();
}

//提示框方法
//tagType 1普通文字提示 2logding界面 3正确提示 4错误提示 5警告提示 6教程展示
//content1 提示文本1
//content2 提示文本2
//button1Text 左边按钮文字
//button1Function 左边按钮触发方法
//button2Text 右边按钮文字
//button2Function 右边按钮触发方法
//button3Text 占满宽按钮文字
//button3Function 占满宽按钮方法
$.popTagShow = function(tagType,content1,content2,button1Text,button1Function,button2Text,button2Function,button3Text,button3Function){
	$('.pop').show();
	$('.pop .pop-content1').hide();
	$('.pop .pop-content2').hide();	
	$('.pop .pop-loding').hide();	
	$('.pop .pop-point').hide();
	$('.pop .pop-teach').hide();
	$('.pop .pop-button-group').hide();
	$(".pop .pop-button3").hide();
	if (tagType == 1) {
		$('.pop .pop-content1').text(content1);
		$('.pop .pop-content2').text(content2);
		$('.pop .pop-content1').show();
		$('.pop .pop-content2').show();
	}
	else if(tagType == 2){
		$('.pop .pop-content1').text(content1);
		$('.pop .pop-content2').text(content2);
		$('.pop .pop-content1').show();
		$('.pop .pop-content2').show();		
		$('.pop .pop-loding').show();
		lodingActiveMovie = window.setInterval("$.lodingActive()",400);
	}
	else if(tagType == 3){
		$('.pop .pop-point img').attr('src','/static/images/right-icon.png');
		$('.pop .pop-point p').text(content1).css('color','#00CEFC');;
		$('.pop .pop-point').show();
		var activeHide = setTimeout("$.popTagHide()",5000)
	}
	else if(tagType == 4){
		$('.pop .pop-point img').attr('src','/static/images/wrong-icon.png');
		$('.pop .pop-point p').text(content1).css('color','#FF0322');
		$('.pop .pop-point').show();
	}	
	else if(tagType == 5){
		$('.pop .pop-point img').attr('src','/static/images/active-icon.png');
		$('.pop .pop-point p').text(content1).css('color','#FFB832');;
		$('.pop .pop-point').show();
	}	
	else if(tagType == 6){
		$('.pop .pop-teach').show();
	}

	if(button1Text != null){
		$('.pop .pop-button-group').show();
		$('.pop .pop-button1').text(button1Text);
		$('.pop .pop-button2').text(button2Text);

		$(".pop .pop-button1").unbind( "click" )
		$(".pop .pop-button2").unbind( "click" )
		$('.pop .pop-button1').click(button1Function)
		$('.pop .pop-button2').click(button2Function)
	}
	else if(button3Text != null){
		$(".pop .pop-button3").show();
		$('.pop .pop-button3').text(button3Text);

		$(".pop .pop-button3").unbind( "click" )
		$('.pop .pop-button3').click(button3Function);
	}
}

function getQueryString(thisUrl,name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	    //var r = window.location.search.substr(1).match(reg);
	var r1 = thisUrl.toString().split("?")[1];
	if(r1 != null){
	    var r = r1.match(reg);
	    if (r != null) return unescape(r[2]);
	}
	return null;

}

